package lc2;
import java.util.*;
import org.junit.*;

public class Ex57 {
    class Solution {
        public int[][] findContinuousSequence(int target) {
            List<List<Integer>> res = new ArrayList<>();
            int lo = 1, hi = 1, sum = 0;
            Deque<Integer> deque = new ArrayDeque<>();
            while (lo <= target) {
                while (sum < target) {
                    sum += hi;
                    deque.offerLast(hi);
                    hi++;
                }
                while (sum > target) {
                    sum -= lo;
                    deque.pollFirst();
                    lo++;
                }
                if (sum == target) {
                    if (deque.size() > 1) {
                        res.add(new ArrayList<>(deque));
                        deque.pollFirst();
                        sum -= lo;
                        lo++;
                    } else {
                        break;
                    }
                }
            }
            int[][] ans = new int[res.size()][];
            int i = 0;
            for (List<Integer> list : res) {
                ans[i++] = list.stream().mapToInt(Integer::valueOf).toArray();
            }
            return ans;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.findContinuousSequence(9));        
    }
}
